package com.itheima.service.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> selectAll() {
        InputStream is = null;
        List<Student> students = null;
        SqlSession sqlSession = null;
        try {
            is = Resources.getResourceAsStream("Mybatits_config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            students = mapper.selectAll();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return students;
    }

    @Override
    public Student selectById(String number) {
        return null;
    }

    @Override
    public void addStudent(Student student) {
        InputStream is =null;
        SqlSession sqlSession = null;
        try {
            //记载配置
            is = Resources.getResourceAsStream("MyBatis_config.xml");
            //
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //
            sqlSession = sqlSessionFactory.openSession(true);
            //
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //
            mapper.addStudent(student);

            //测试
            System.out.println("测试1");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override

    public void updateStudent(Student student) {
        InputStream is = null;
        SqlSession sqlSession = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.获取StudentMapper接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.调用实现类对象修改方法
            mapper.updateStudent(student);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void deleteStudent(String number) {
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //获取StudentMapper接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //调用StudentMapper里的删除方法
            mapper.deleteStudent(number);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
